<template>
  <div>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb-10px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['oa:accounting-companies:create']"
        >
          <Icon icon="ep:plus" /> 新增调价
        </el-button>
      </el-col>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" empty-text="暂无调价记录">
      <el-table-column label="服务费起始月份" align="center" prop="adjustYear" min-width="180">
        <template #default="scope">
          <el-icon class="mr-5px"><Clock /></el-icon>
          {{ formatAdjustTime(scope.row.adjustYear, scope.row.adjustMonth) }}
        </template>
      </el-table-column>
      <el-table-column label="收费价格(元)" align="center" prop="price" min-width="120">
        <template #default="scope">
          {{scope.row.price}}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" min-width="120" />
      <el-table-column label="更新时间" align="center" prop="updateTime" min-width="160">
        <template #default="scope">
          {{ formatDate(scope.row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="updaterName" min-width="100" />
      <el-table-column label="操作" align="center" min-width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row)"
            v-hasPermi="['oa:accounting-companies:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oa:accounting-companies:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表单弹窗 -->
    <PriceAdjustmentForm ref="formRef" @success="getList" />
  </div>
</template>

<script setup lang="ts">
import { PriceAdjustmentApi, PriceAdjustmentVO } from '@/api/oa/accountingcompanies/priceAdjustment'
import PriceAdjustmentForm from './PriceAdjustmentForm.vue'
import { formatDate } from '@/utils/formatTime'

/** 记账企业调价记录列表 */
defineOptions({ name: 'PriceAdjustmentList' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  companyId: number
}>()

const loading = ref(false) // 列表的加载中
const list = ref<PriceAdjustmentVO[]>([]) // 列表的数据
const formRef = ref() // 表单 Ref

/** 查询列表 */
const getList = async () => {
  // 如果 companyId 无效，则不执行查询
  if (!props.companyId || props.companyId <= 0) {
    list.value = []
    return
  }
  
  loading.value = true
  try {
    list.value = await PriceAdjustmentApi.getPriceAdjustmentList(props.companyId)
  } finally {
    loading.value = false
  }
}

/** 打开表单 */
const openForm = (type: string, data?: PriceAdjustmentVO) => {
  formRef.value?.open(type, data, { companyId: props.companyId })
}

/** 删除按钮操作 */
const handleDelete = async (row: PriceAdjustmentVO) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PriceAdjustmentApi.deletePriceAdjustment(row.id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch (error) {
    console.error('删除操作失败:', error)
  }
}

/** 格式化调价时间显示 */
const formatAdjustTime = (year: number, month: number) => {
  if (!year || !month) return ''
  return `${year}年${month.toString().padStart(2, '0')}月`
}

// 监听 companyId 变化
watch(() => props.companyId, (newCompanyId, oldCompanyId) => {
  console.log('PriceAdjustmentList - companyId changed:', { oldCompanyId, newCompanyId })
  if (newCompanyId && newCompanyId > 0) {
    getList()
  } else {
    list.value = []
  }
}, { immediate: true })
</script> 